/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package metier;

import exception.NotValidKey;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author kluck
 */
public class ResponseForm {
    protected ClaimType claimType;
    protected HashMap<String, String> fields;
    protected ArrayList<String> requiredFields;
    
    public ResponseForm(ClaimType claimType) {
        this.claimType = claimType;
        this.fields = new HashMap();
        this.requiredFields = new ArrayList();
        this.requiredFields.add("Garage reference");
        this.requiredFields.add("Car registration");
        if (claimType == ClaimType.COMPLEX) {
            this.requiredFields.add("Detailed accident description");
            this.requiredFields.add("Other person name");
            this.requiredFields.add("Other person insurance company");
        }
        for (String str : this.requiredFields) {
            this.fields.put(str, "");
        }
    }
    
    public String generateFormText(String customerName, String employeeName,
            GeneratedType generatedType) {
        StringBuilder result = new StringBuilder();
        result.append("Dear ").append(customerName).append(",\n").append(
                "Please fill this form to help us in our reimbursement process.\n");
        switch(generatedType) {
            case LETTER:
                for (String key : this.getFields().keySet()) {
                    // If the field is already completed, we skip it in the form
                    if ("".equals(this.getFields().get(key))) {
                        result.append(key).append(" :\n");
                    }
                }
                break;
            case EMAIL:
                result = new StringBuilder();
                result.append("Dear ").append(customerName).append(",\n").
                        append("Click on the following link and fill in the " +
                        "information asked.\n" +
                        "<a href=\"http://company-site.com/claim.html\">Link</a>\n");
                break;
            case WEBFORM:
                result.append("<form>\n");
                for (String key : this.getFields().keySet()) {
                    if ("".equals(this.getFields().get(key))) {
                    result.append(key).
                            append(" : <input type=\"text\" name=\"").
                            append(key).
                            append("\"/><br/>\n");
                            }
                }
        result.append("</form>\n");
                break;
        }
        result.append("Sincerly yours,\n").append(employeeName);
        return result.toString();
    }

    public ArrayList<String> getRequiredFields() {
        return requiredFields;
    }
    
    public ClaimType getClaimType() {
        return claimType;
    }
    
    public HashMap<String,String> getFields() {
        return fields;
    }
    
    public void setField(String key, String value) throws NotValidKey {
        if (this.getRequiredFields().contains(key)) {
            this.getFields().put(key, value);
        } else {
            throw new NotValidKey();
        }
    }
    
    public boolean isFormComplete() {
        boolean result = true;
        for (String key : this.getFields().keySet()) {
            if ("".equals(this.getFields().get(key))) {
                result = false;
            }
        }
        return result;
    }
    
    public enum ClaimType {
        SIMPLE, COMPLEX;
    }
    
    public enum GeneratedType {
        LETTER, EMAIL, WEBFORM;
    }
}
